package com.graduation.roombooking.controller.web;

import com.graduation.roombooking.service.IOrderService;
import com.graduation.roombooking.service.UserService;
import com.graduation.roombooking.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/5/4 20:54
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/web/order")
@Api(tags = "web——订单模块")
public class WebOrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private UserService userService;

    @ApiOperation("订单列表")
    @ResponseBody
    @PostMapping("/list")
    public RespBean getList(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("system_administrator")) {
                    return orderService.getList(null);
                }else if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                    return orderService.getList(userVo.getCompanyName());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("修改订单状态")
    @ResponseBody
    @PostMapping("/update/status")
    public RespBean updateStatus(@RequestBody UpdateOrderStatusVo updateOrderStatusVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=updateOrderStatusVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                    return orderService.updateStatus(userVo.getCompanyName(),updateOrderStatusVo.getStatus(),updateOrderStatusVo.getOrderId());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非组织用户，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("联合查询")
    @ResponseBody
    @PostMapping("/union/query")
    public RespBean unionQuery(@RequestBody UnionQueryOrderVo unionQueryOrderVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=unionQueryOrderVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("system_administrator")) {
                    return orderService.unionQuery(unionQueryOrderVo,null);
                }else if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                    return orderService.unionQuery(unionQueryOrderVo,userVo.getCompanyName());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("数据统计:统计七天内本部门各会议室的使用次数(系统管理员传入部门名称即可查看其余部门的使用次数)")
    @ResponseBody
    @PostMapping("/statistics")
    public RespBean getStatistics(@RequestBody StatisticsVo statisticsVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=statisticsVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("system_administrator")) {
                    return orderService.getStatistics(statisticsVo.getCompanyName(),statisticsVo.getDay());
                }else if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                    return orderService.getStatistics(userVo.getCompanyName(),statisticsVo.getDay());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

}
